{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "相似文本、相似度和文本中词的数量：\n",
      "他 很 注重 教育 ，  0.872618556022644 5\n",
      "长 得 怎么样 ？  0.8633190989494324 4\n",
      "学到 很多 科学 知识 。  0.8551362156867981 5\n",
      "整体 来 说 ，  0.8541723489761353 4\n",
      "我 不能 接受 这样 的 思想 。  0.853570282459259 7\n",
      "“ 这样 的 学习 是 真实 的 ，  0.8509118556976318 8\n",
      "他们 在 制度 管理 和 权威 管理 的 基础 上 ， 追求 的 是 道德 管理 。  0.8504587411880493 17\n",
      "我们 搞 社会 大 文化 建设 不可 忽视 礼仪 文化 。  0.8472367525100708 11\n",
      "过去 主要 是 学习 文化 。  0.8466610312461853 6\n",
      "主要 是 教育 普及 ，  0.8463680148124695 5\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_13380\\1038890330.py:29: DeprecationWarning: Call to deprecated `docvecs` (The `docvecs` property has been renamed `dv`.).\n",
      "  sims = model_dm.docvecs.most_similar([inferred_vector_dm], topn = 10)\t\t\t\t\t\t#找到与测试文本向量最相似的其他文本向量\n"
     ]
    }
   ],
   "source": [
    "import gensim\t\t\t\t\t\t\t#导入需要的库与模块\n",
    "from gensim.models.doc2vec import Doc2Vec\n",
    "#从gensim.models.doc2vec模块中导入TaggedDocument类\n",
    "TaggededDocument = gensim.models.doc2vec.TaggedDocument\n",
    "def get_data():\t\t\t\t\t\t\t#定义读取数据函数\n",
    "\twith open(\"data/train.txt\", 'r', encoding = 'utf-8') as f:\n",
    "\t\tdocs = f.readlines()\t\t\t#读取每行数据\n",
    "\ttrain_data = []\n",
    "\tfor i, text in enumerate(docs):\n",
    "\t\tword_list = text.split(' ')\t#按空格分割\n",
    "\t\t#删除每行最后一个单词后的多余空格或换行符\n",
    "\t\tword_list[len(word_list)-1] = word_list[len(word_list)-1].strip()\n",
    "\t\t#使用词列表和文档索引作为参数，实例化TaggededDocument类的对象\n",
    "\t\tdocument = TaggededDocument(word_list, tags = [i])\n",
    "\t\ttrain_data.append(document)\n",
    "\treturn train_data\n",
    "def train_model(x_train):\t\t\t\t#训练Doc2Vec模型\n",
    "\t#创建Doc2Vec类的对象\n",
    "\tmodel_dm = Doc2Vec(x_train, min_count = 1, window = 3, vector_size = 20, negative = 5, workers = 4, dm = 1)\n",
    "\tmodel_dm.train(x_train, total_examples = model_dm.corpus_count, epochs = 70)\t\t\t\t\t\t\t\t\t\t\t\t\t#训练模型\n",
    "\tmodel_dm.save(\"data/model_doc2vec\")\t\t\t\t#保存模型\n",
    "\treturn model_dm\n",
    "def test():\n",
    "\tmodel_dm = Doc2Vec.load(\"data/model_doc2vec\")\t#加载模型\n",
    "\t#定义一个测试文本列表\n",
    "\ttest_text = ['科学', '教育', '是', '难搞', '的']\n",
    "\t#使用模型推断测试文本的向量表示\n",
    "\tinferred_vector_dm = model_dm.infer_vector(test_text)\n",
    "\tsims = model_dm.docvecs.most_similar([inferred_vector_dm], topn = 10)\t\t\t\t\t\t#找到与测试文本向量最相似的其他文本向量\n",
    "\treturn sims\n",
    "if __name__ == '__main__':\n",
    "\ttrain_data = get_data()\n",
    "\tmodel_dm = train_model(train_data)\n",
    "\tsims = test()\n",
    "\tprint(\"相似文本、相似度和文本中词的数量：\")\n",
    "\tfor count, sim in sims:\t\t\t\t\t\t#遍历相似度列表\n",
    "\t\tsentence = train_data[count]\t\t\t#获取相似度最高的文本\n",
    "\t\twords = ''\n",
    "\t\tfor word in sentence[0]:\t\t\t\t#遍历文本中的每个词\n",
    "\t\t\twords = words + word + ' '\n",
    "\t\t#输出相似文本、相似度和文本中词的数量\n",
    "\t\tprint(words, sim, len(sentence[0]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
